﻿#if UNITY_EDITOR
using System.Reflection;
using CriWare;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEditor;
using UnityEngine;
using UnityEngine.Playables;

namespace IQIGame.Onigao.GamePlay
{
    [ExecuteInEditMode]
    public class StoryEditorMainUpdate : EditorSceneUpdate
    {
        public static StoryEditorMainUpdate Instance => instance as StoryEditorMainUpdate;

        public ObjectBinding editorUIBinding;

        public UIStory ui;
        public PlayableDirector playable;
        public ExRawImage videoPanel;

        private VideoPlayerForUI _videoPlayer;

        protected override void OnInitialize()
        {
            this.InitUI();
            if (this.playable)
            {
                this.playable.Stop();
            }
            if (CriManaPlugin.IsLibraryInitialized() == false)
            {
                CriWareInitializer cfg = GameObject.FindObjectOfType<CriWareInitializer>();
                if (cfg != null)
                {
                    CriWareInitializer.InitializeMana(cfg.manaConfig);
                }
                else
                {
                    CriWareInitializer.InitializeMana(new CriManaConfig());
                    Debug.Log("[CRIWARE] Timeline / Mana: Can't find CriWareInitializer component; Using default parameters in edit mode.");
                }
            }
        }

        private void InitUI()
        {
            if (this.ui != null)
            {
                this.ui.Clear();
            }
            else
            {
                if (!Application.isPlaying)
                {
                    PrefabUtility.RevertPrefabInstance(this.editorUIBinding.gameObject, InteractionMode.AutomatedAction);
                }
                this.ui = new UIStory();
                this.ui.Init(this.editorUIBinding.gameObject);

                var rOptionUITrans = this.ui.optionItemPrefab.transform;
                var rParent = rOptionUITrans.parent;
                foreach (Transform rTrans in rParent)
                {
                    if (rTrans != rOptionUITrans)
                    {
                        GameObject.DestroyImmediate(rTrans.gameObject);
                    }
                }
                // var rSpeakerUITrans = this.ui.speakerItemPrefab.transform;
                // var rSpeakerParent = rSpeakerUITrans.parent;
                // foreach (Transform rTrans in rSpeakerParent)
                // {
                //     if (rTrans != rSpeakerUITrans)
                //     {
                //         GameObject.DestroyImmediate(rTrans.gameObject);
                //     }
                // }
            }
            this.ui.Show(null);
        }

        public VideoPlayerForUI GetOrCreateVideoPlayer()
        {
            if (this._videoPlayer == null)
            {
                this._videoPlayer = new VideoPlayerForUI();
                this._videoPlayer.InitPlayer(null, false, false);
                this._videoPlayer.SetVideoTarget(StoryEditorMainUpdate.Instance.videoPanel);
                this.OnUpdate += () =>
                {
                    if (this._videoPlayer != null)
                    {
                        this._videoPlayer.OnUpdate(Time.deltaTime);
                    }
                };
            }
            return this._videoPlayer;
        }
    }
}
#endif